//
//  SignUpViewController.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/8/22.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit
import AFNetworking

class SignUpViewController: SignViewController {
    
    fileprivate let phoneView = InputView.init(inputStyle: .phone) //账号
    fileprivate let codeView = InputView.init(inputStyle: .code) //验证码
    fileprivate let passwordView = InputView.init(inputStyle: .password) //密码
    fileprivate let confirmView = InputView.init(inputStyle: .password) //确认密码
    
    fileprivate let signUpButton = UIButton() //登录

    override func viewDidLoad() {
        super.viewDidLoad()

            addSubView()

    }
    
    //MARK: 添加子视图
    fileprivate func addSubView() {
        
        let headView = SignHeadView.init(frame: .init(x: 0, y: 0, width: ScreenW, height: RATIO_H(maxNum: 280)))
        self.view.addSubview(headView)
        
        signUpButton.setTitle(signUp_title, for: .normal)
        signUpButton.setTitleColor(UIColor.white, for: .normal)
        signUpButton.backgroundColor = UIColor.lightGray
        signUpButton.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 15))
        signUpButton.layer.cornerRadius = RATIO_H(maxNum: 20)
        signUpButton.layer.masksToBounds = true
        signUpButton.isEnabled = false
        signUpButton.addTarget(self, action: #selector(signUpAction(sender:)), for: .touchUpInside)
        self.view.addSubview(signUpButton)
        
        passwordView.placeholder = rightInputPassword
        confirmView.placeholder = inputPasswardAgain
        passwordView.textField.isSecureTextEntry = true //密码不可见
        confirmView.textField.isSecureTextEntry = true //密码不可见
        
        phoneView.textDelegate = self
        codeView.textDelegate = self
        passwordView.textDelegate = self
        confirmView.textDelegate = self
        
        self.view.addSubview(phoneView)
        self.view.addSubview(codeView)
        self.view.addSubview(passwordView)
        self.view.addSubview(confirmView)
        
        phoneView.snp.makeConstraints { (make) in
            make.top.equalTo(headView.snp.bottom).offset(RATIO_H(maxNum: -20))
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 50))
        }
        
        codeView.snp.makeConstraints { (make) in
            make.top.equalTo(phoneView.snp.bottom).offset(RATIO_H(maxNum: 20))
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(phoneView)
        }
        
        passwordView.snp.makeConstraints { (make) in
            make.top.equalTo(codeView.snp.bottom).offset(RATIO_H(maxNum: 20))
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(codeView)
        }
        
        confirmView.snp.makeConstraints { (make) in
            make.top.equalTo(passwordView.snp.bottom).offset(RATIO_H(maxNum: 20))
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(passwordView)
        }
        
        signUpButton.snp.makeConstraints { (make) in
            make.top.equalTo(confirmView.snp.bottom).offset(RATIO_H(maxNum: 30))
            make.centerX.equalToSuperview()
            make.width.equalTo(RATIO(maxNum: 300))
            make.height.equalTo(RATIO_H(maxNum: 40))
        }
    }
    
    //MARK: 注册
    @objc fileprivate func signUpAction(sender:UIButton) {
        
        if sender.backgroundColor != UIColor.red {
            return
        }
        
        KMTool.waiting()
        KMTool.requestMethod(.post, URLString: url_signUp, parameters: ["user_name":phoneView.text,"code":codeView.text,"password":passwordView.text,"repassword":confirmView.text], success: { (value, json) in
            
            KMTool.stop()
            KMTool.showText(_text: json["msg"].stringValue)
            
            if json["status"].stringValue == "0" {
                KMTool.getUserNews(userName: self.phoneView.text, success: {
                    KMTool.setObject(self.phoneView.text, forEnumKey: .phone)
                    self.navigationController?.popToRootViewController(animated: true)
                })
                
            }
            
        }) {
            KMTool.stop()
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}


extension SignUpViewController : TextDelegate {
    
    //MARK: 监听输入文本
    func monitorText() {
        
        if phoneView.textField.text != "" && codeView.textField.text != "" && passwordView.textField.text != "" && confirmView.textField.text != "" {
            
            signUpButton.backgroundColor = UIColor.red
            signUpButton.isEnabled = true
        }else{
            signUpButton.backgroundColor = UIColor.lightGray
            signUpButton.isEnabled = false
        }
    }
    
    //MARK: 获取验证码
    func getPhoneCode() {
        
        if phoneView.text == "" {
            KMTool.showText(_text: inputPhone)
            return
        }
        
        KMTool.waiting()
        
        KMTool.requestMethod(.post, URLString: url_signUpCode, parameters: ["user_name":phoneView.text], success: { (value, json) in
            
            KMTool.stop()
            self.codeView.codeBlock!((json.dictionaryObject)!)//根据发送验证码情况做相应处理
        }) {
            KMTool.stop()
        }
        
    }
    
}
